﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using ESRI.ArcGIS.Geoprocessor;
//using ESRI.ArcGIS.AnalysisTools;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Controls;

namespace ISchool
{
    class SpaceCalCulation
    {
        //田桂芳
        //描述：距离测量和面积测量
        
        //设定控件
        private AxMapControl IMapControl;
        //设定显示ListBox控件
        private System.Windows.Forms.ListBox IListBox;

        IPoint pPointStd;
        private static List<double> st = new List<double>(100);
        private  static IPointCollection pc;

        public IPolyline pl_line = new PolylineClass();
        public IPolygon pl_area = new PolygonClass();
        public SpaceCalCulation(AxMapControl test, System.Windows.Forms.ListBox listBox)
        {
            IMapControl = test;
            IListBox = listBox;
        }
        //根据鼠标坐标，计算距离
        public double Idistance(int e_x,int e_y)
        {
            //获取当前控件中的地图
            IMap pMap = IMapControl.Map;
            //新建点
            IPoint pPoint = new PointClass();
            //获取地图的控件坐标体系
            pPoint.SpatialReference = pMap.SpatialReference;
            //屏幕点转地图坐标
            pPoint = IMapControl.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e_x, e_y);
            //已有线转点集
            pc = (IPointCollection)pl_line;

            object mi = Type.Missing;
            object ma = Type.Missing;
            //点集增加新建的点
            pc.AddPoint(pPoint, ref mi, ref ma);
            //距离缓存变量
            double temp=0;

            if (pPointStd == null)
            {
                //第一个点的绘制
                pPointStd = pPoint;
                // 新建图像显示控制的方法
                IScreenDisplay screenDisplay = IMapControl.ActiveView.ScreenDisplay;
                //绘制方法
                screenDisplay.StartDrawing(screenDisplay.hDC, (short)esriScreenCache.esriNoScreenCache);
                //设置点的样式
                screenDisplay.SetSymbol(new SimpleMarkerSymbolClass());
                //绘制点
                screenDisplay.DrawPoint(pPointStd);
                //完成绘制
                screenDisplay.FinishDrawing();
            }

            else
            {
                //同上
                IScreenDisplay screenDisplay = IMapControl.ActiveView.ScreenDisplay;
                screenDisplay.StartDrawing(screenDisplay.hDC, (short)esriScreenCache.esriNoScreenCache);
                screenDisplay.SetSymbol(new SimpleMarkerSymbolClass());
                screenDisplay.DrawPoint(pPoint);
                screenDisplay.FinishDrawing();
                //新建获取两个地理要素距离的方法
                IProximityOperator pProximity;
                //获取上一个点
                pProximity = pPointStd as IProximityOperator;
                //距离
                double pDistance;
                //新建点与上一个点之间的距离
                pDistance = pProximity.ReturnDistance(pPoint);
                // 新建点变成上一个点（供下次使用）
                pPointStd = pPoint;
                //距离存入链表
                st.Add(pDistance);
                //遍历并获取总距离
                double sl = 0;
                for (int i = 0; i < st.Count; i++)
                {
                    sl += st[i];
                }
                temp = sl;
                double temp2 = st[st.Count - 1];
                //由度转换成米
                temp *= 113480.591;
                temp2 *= 113480.591;
                //显示
                IListBox.Items.Add("点的总数 " + pc.PointCount.ToString());
                if (temp > 1000)
                {
                    temp = temp / 1000;
                    IListBox.Items.Add("总长度" + temp.ToString(".##") + "公里");
                }
                else
                {
                    IListBox.Items.Add("总长度" + temp.ToString(".##") + "米");
                }
                if (temp2 > 1000)
                {
                    temp2 = temp2 / 1000;
                    IListBox.Items.Add("此段长度" + temp2.ToString(".###") + "公里");
                }
                else
                {

                    IListBox.Items.Add("此段长度" + temp2.ToString(".###") + "米");
                }

            }
            return temp;
        }
        public double IArea(int e_x,int e_y)
        {
            //同上
            IMap pMap = IMapControl.Map;
            IPoint pPoint = new PointClass();
            pPoint.SpatialReference = pMap.SpatialReference;
            pPoint = IMapControl.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e_x, e_y);
            //将面转换成点集
            pc = (IPointCollection)pl_area;

            object mi = Type.Missing;
            object ma = Type.Missing;

            pc.AddPoint(pPoint, ref mi, ref ma);
            double temp = 0;
            if (pc.PointCount<=2)
            {
                //第一个点的绘制
                pPointStd = pPoint;
                // 同上
                IScreenDisplay screenDisplay = IMapControl.ActiveView.ScreenDisplay;
                screenDisplay.StartDrawing(screenDisplay.hDC, (short)esriScreenCache.esriNoScreenCache);
                screenDisplay.SetSymbol(new SimpleMarkerSymbolClass());
                screenDisplay.DrawPoint(pPointStd);
                screenDisplay.FinishDrawing();
            }
            else
            {
                IScreenDisplay screenDisplay = IMapControl.ActiveView.ScreenDisplay;

                screenDisplay.StartDrawing(screenDisplay.hDC, (short)esriScreenCache.esriNoScreenCache);

                screenDisplay.SetSymbol(new SimpleMarkerSymbolClass());

                screenDisplay.DrawPoint(pPoint);

                screenDisplay.FinishDrawing();
                //将新增加点后的点集再转换成多边形
                pl_area= (IPolygon)pc;
                //多边形转换成面
                IArea pArea = (IArea)pl_area;
                //获取面积
                double m_dArea = pArea.Area;
                //地理转换，两次方
                m_dArea = System.Math.Abs(m_dArea) * 113480.591 * 113480.591;
                MessageBox.Show(m_dArea.ToString(".###") + "平方米");
  
            }
           
            return temp;
        }

    }
}
